<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/2
 * Time: 21:36
 * 购物车
 */

namespace app\common\model;

use think\Model;

class Cart extends Model
{
	// 追加属性
	protected $append = [
		'create_time_text',
		'update_time_text',
		'status_text'
	];
	
	const STATUS_DEL = 0;
	const STATUS_Y = 1;
	const STATUS_BUY = 2;
	
	
	public static $_status = [
		self::STATUS_DEL => '已删除',
		self::STATUS_Y => '正常',
		self::STATUS_BUY => '已购买',
	];
	
	// 表名
	protected $name = 'cart';
	protected $autoWriteTimestamp = true;
	
	
	public function getStatusTextAttr($value, $data) {
		$value = $value ? $value : $data['status'];
		$list = self::$_status;
		return isset($list[$value]) ? $list[$value] : '';
	}
	
	
	public function getCreateTimeTextAttr($value, $data)
	{
		$value = $value ? $value : $data['create_time'];
		return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
	}
	
	
	public function getUpdateTimeTextAttr($value, $data)
	{
		$value = $value ? $value : $data['update_time'];
		return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
	}
	
	public function user(){
		return $this->belongsTo('user', 'user_id', 'id', '', 'left')->setEagerlyType(0);
	}
	
	public function goods(){
		return $this->belongsTo('goods', 'goods_id', 'id', '', 'left')->setEagerlyType(0);
	}
	
	public function spec() {
		return $this->belongsTo('goodsSpecPrice', 'price_id', 'id', '', 'left')->setEagerlyType(0);
	}
}
